home *** CD-ROM | disk | FTP | other *** search
/ CGI How-To / CGI HOW-TO.iso / chap7 / 7_2 / ht72a.txt < prev    next >
Encoding:
Text File  |  1996-06-15  |  2.4 KB  |  99 lines

  1. #!/usr2/local/bin/perl -w
  2.  
  3. use GD;
  4. require "cgilib.pl";
  5.  
  6.  
  7. sub centerString
  8. {
  9.     my ($img,          # gd Image to render in 
  10.        $font,          # gd Font to render with
  11.        $x, $y,         # x and y coordinates of center of string
  12.        $txt,           # text to render
  13.        $colour) = @_;  # colour to render the text in
  14.     
  15.     # Calculate the width of the text in pixels.
  16.     my $width = length($txt)*$font->width;
  17.  
  18.     $img->string($font,1+$x-$width/2,$y,$txt,$colour);
  19. }
  20.  
  21.  
  22. # Output the GIF content type
  23. print "Content-type: image/gif\n\n";
  24.  
  25. # Initialize a hash of CGI arguments
  26. # using routines from cgilib.pl
  27. readParse(*dict);
  28.  
  29. my $barWidth      = $dict{'barWidth'}   || 20;
  30. my $barHeight     = $dict{'barHeight'}  || 100;
  31. my $barPadding    = $dict{'barPadding'} || 2;
  32. my @score = split / /,($dict{'score'} || '1 2 3 4 5 6 7 8 9 10');
  33.  
  34. my $topPadding    = 15;
  35. my $bottomPadding = 20;
  36.  
  37. my $xAxis         = $topPadding+$barHeight;
  38. my $width         = 10*$barWidth+11*$barPadding;
  39. my $height        = $barHeight+$topPadding+$bottomPadding;
  40.  
  41. # Create a blank gd image of the required size 
  42. my $im = new GD::Image($width,$height);
  43.  
  44. # Allocate colors 
  45. my $black = $im->colorAllocate(  0,  0,  0);
  46. my $white = $im->colorAllocate(255,255,255);
  47. my $red   = $im->colorAllocate(255,0,0);
  48.  
  49. # Make the image interlaced
  50. $im->interlaced('true');
  51.  
  52. # Draw the X-axis
  53. $im->line(0,$xAxis,$width-1,$xAxis,$white);
  54.  
  55. # Determine the height of the highest bar
  56. for ( $max=0, $i=0 ; $i<10 ; $i++ )
  57. {
  58.     $max = $score[$i] if ($score[$i] > $max);
  59. }
  60.  
  61. # For each of the 10 bars
  62. for ( $i=0 ; $i<10 ; $i++)
  63. {
  64.     # Calculate the X coordinate for the center of the bar
  65.     $x = ($i+0.5)*$barWidth+($i+1)*$barPadding;
  66.  
  67.     # calculate how high this bar should be
  68.     $barLen = $score[$i]*$barHeight/$max;
  69.  
  70.     # draw a tick mark on the x-axis
  71.     $im->line($x,$xAxis+1,$x,$xAxis+2,$white);
  72.  
  73.     # label the x-axis with numbers from 1 to 10
  74.     # using the bold font
  75.     centerString($im,
  76.                  gdMediumBoldFont,  
  77.          $x, $xAxis+3,      # X, Y coordinates
  78.          $i+1,              # text to display
  79.          $white);
  80.  
  81.     # label the height of the bar using the small font
  82.     centerString($im,
  83.                  gdSmallFont,
  84.          $x, $xAxis-$barLen-11,
  85.                  $score[$i],
  86.          $white);
  87.  
  88.     # draw one bar
  89.     $im->filledRectangle( 
  90.      $x-$barWidth/2, $xAxis-$barLen,   
  91.      $x+$barWidth/2, $xAxis-1,         
  92.      $red);
  93. }
  94.  
  95. # Convert the image to GIF and print it to standard output
  96. print $im->gif;
  97.  
  98. 1;
  99.